package com.cardbaobao.cardbabyclient.ui.pop;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;
import com.cardbaobao.cardbabyclient.widget.seekBar.BubbleSeekBar;

/**
 * Created by Jiangzx on 14:49.
 */

public class LoanAmountPop extends PopupWindow {
    private static final int MAX=200000;//SeekBar展示的最大值
    private static final int MAX_PROGRESS=200;//SeekBar展示的最大刻度
    public interface LoanAmountEnsuredListener{
        void onLoanAmountEnsured(int ensuredAmount);
    }
    private LoanAmountEnsuredListener loanAmountEnsuredListener;
    public void setLoanAmountEnsuredListener(LoanAmountEnsuredListener listener){
        this.loanAmountEnsuredListener=listener;
    }

    private int screenWidth;
    private TextView tvReset;
    private TextView tvEnsured;
    private TextView tvAmount;
    private TextView tvAmountPop;
    private BubbleSeekBar seekBar;
    private ImageButton ibtReduce;
    private ImageButton ibtIncrease;
    private int ensuredAmount;
    public LoanAmountPop(Context context){
        View rootView= LayoutInflater.from(context).inflate(R.layout.pop_loan_amount,null);
        rootView.findViewById(R.id.view_empty).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tvReset= (TextView) rootView.findViewById(R.id.tv_reset);
        tvEnsured= (TextView) rootView.findViewById(R.id.tv_ensure);
        tvAmount= (TextView) rootView.findViewById(R.id.tv_amount);
        tvAmountPop= (TextView) rootView.findViewById(R.id.tv_amount_pop);
        seekBar= (BubbleSeekBar) rootView.findViewById(R.id.seek_bar);
        ibtReduce= (ImageButton) rootView.findViewById(R.id.ibt_reduce);
        ibtIncrease= (ImageButton) rootView.findViewById(R.id.ibt_increase);
        initListener();
        setAnimationStyle(R.style.CustomTopAnimateStyle);
        setBackgroundDrawable(new ColorDrawable());
        setOutsideTouchable(false);
        screenWidth=ScreenUtil.getScreenWidth(context);
        setWidth(screenWidth);
        //设置内容区域高度为屏幕高度的一半
        rootView.findViewById(R.id.pop_content).getLayoutParams().height=
                (int) (ScreenUtil.getScreenHeight(context)*0.5f);
        setContentView(rootView);
    }

    public void setCurrent(int amount){
        if(amount>0){
            this.ensuredAmount=amount;
            //计算进度
            int progress= (int) ((this.ensuredAmount/(float)MAX)*MAX_PROGRESS);
            seekBar.setProgress(progress);
        }else {
            this.ensuredAmount=0;
            seekBar.setProgress(0);
        }
        tvAmount.setText(String.valueOf(ensuredAmount));
        tvAmountPop.setText(String.valueOf(ensuredAmount));
    }

    private void initListener(){
        tvReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                seekBar.setProgress(0);
                tvAmountPop.setText("0");
                tvAmount.setText("0");
            }
        });
        tvEnsured.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                ensuredAmount=seekBar.getProgress()*1000;
                tvAmount.setText(String.valueOf(ensuredAmount));
                if(loanAmountEnsuredListener!=null){
                    loanAmountEnsuredListener.onLoanAmountEnsured(ensuredAmount);
                }
            }
        });
        ibtReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int progress=seekBar.getProgress();
                progress--;
                if(progress>=0){
                    seekBar.setProgress(progress);
                    setText(progress);
                }
            }
        });
        ibtIncrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int progress=seekBar.getProgress();
                progress++;
                if(progress<=seekBar.getMax()){
                    seekBar.setProgress(progress);
                    setText(progress);
                }
            }
        });
        seekBar.setSeekBarChangedListener(new BubbleSeekBar.SeekBarChangedListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                tvAmountPop.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar, Rect bounds) {
                tvAmountPop.setVisibility(View.VISIBLE);
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, Rect bounds, int progress, boolean fromUser) {
                if(fromUser){
                    setText(progress);
                    int width=tvAmountPop.getWidth();
                    LinearLayout.LayoutParams params=
                            (LinearLayout.LayoutParams) tvAmountPop.getLayoutParams();
                    int marginLeft=bounds.left+
                            ((int)((bounds.right-bounds.left)/2.0f));
                    if(marginLeft>0&&(marginLeft+width<screenWidth)){
                        params.leftMargin=marginLeft;
                    }
                    tvAmountPop.setLayoutParams(params);
                }
            }
        });
    }

    private void setText(int seekBarProgress){
        int tempValue=seekBarProgress*1000;
        tvAmountPop.setText(String.valueOf(tempValue));
        tvAmount.setText(String.valueOf(tempValue));
    }
}
